<template>
	<view >
		<view class="block-box padlr24 padtb10 mar-t20">
			<u--form
				labelPosition="left"
				:model="formData"
				:rules="rules"
				ref="uForm"
				:labelStyle="{padding: '12rpx 0'}"
				labelWidth="160rpx"
			>
				<u-form-item
					:label="item.label"
					:prop="item.key"
					:borderBottom="index < cellList.length - 1"
					v-for="(item, index) in cellList"
					:key="index"
				>
					<view class="flex flex-j-end w_100" v-if="item.key == 'avatar'">
						<u-avatar :src="formData[item.avatar]" size="80rpx"></u-avatar>
					</view>
					<view class="flex flex-j-end radio-box w_100" v-if="item.key == 'sex'">
						<u-radio-group
							v-model="formData.sex"
							activeColor="#FF7171"
							size="36rpx"
						>
							<u-radio
							  label="先生"
							  :name="1"
							  labelSize="28rpx"
							>
							</u-radio>
							<u-radio
							  label="女士"
							  :name="0"
							  labelSize="28rpx"
							  :customStyle="{marginLeft: '32rpx'}"
							>
							</u-radio>
						</u-radio-group>
					</view>
					<view 
						class="flex flex-j-end radio-box w_100" 
						v-if="item.key == 'birthday'"
						@click="dateShow = true"
					>
						<u--input
							v-model="formData[item.key]"
							border="none"
							:placeholder="item.placeholder"
							inputAlign="right"
							readonly
						>
							<view class="" slot="suffix">
								<u--image
									:showLoading="true" 
									:src="getImg('arrow-right')" 
									width="16rpx"
									height="24rpx"
								></u--image>
							</view>
						</u--input>
					</view>
					<view class="flex flex-j-end w_100" v-if="!['birthday', 'sex', 'avatar'].includes(item.key)">
						<u--input
							v-model="formData[item.key]"
							border="none"
							:placeholder="item.placeholder"
							inputAlign="right"
						></u--input>
					</view>
				</u-form-item>
			</u--form>
		</view>
		<view class="btn-box flex-center mar-t48">
			<view class="btn flex-center" @click="onSubmit">
				保存编辑
			</view>
		</view>
		
		<u-datetime-picker
			:show="dateShow"
			v-model="dateDefault"
			mode="date"
			title="选择生日"
			@cancel="dateShow = false"
			@confirm="onBirthday"
		></u-datetime-picker>
	</view>
</template>

<script>
	import { getNowTime } from "@/utils/utils.js"
	export default {
		data() {
			return {
				cellList: [
					{
						label: "头像",
						key: "avatar"
					},
					{
						label: "昵称",
						key: "nickName",
						placeholder: "请输入昵称"
					},
					{
						label: "性别",
						key: "sex"
					},
					{
						label: "生日",
						key: "birthday",
						placeholder: "请选择生日"
					},
					{
						label: "手机",
						key: "mobile",
						placeholder: "请输入手机号码"
					},
					{
						label: "简介",
						key: "introduction",
						placeholder: "请输入简介"
					},
				],
				formData: {
					avatar: "",
					nickName: "",
					sex: 1,
					birthday: "",
					mobile: "",
					introduction: ""
				},
				rules: {
					// 字段名称
					avatar: [
						{
							required: true, 
							message: '请上传头像',
							trigger: ['change','blur'],
						},
					],
					nickName: [
						{
							required: true, 
							message: '请输入昵称',
							trigger: ['change','blur'],
						},
					],
					birthday: [
						{
							required: true, 
							message: '请选择生日',
							trigger: ['change','blur'],
						},
					],
					mobile: [
						{
							required: true, 
							message: '请输入手机号码',
							trigger: ['change','blur'],
						},
					],
					introduction: [
						{
							required: true, 
							message: '请输入手机号码',
							trigger: ['change','blur'],
						},
					],
				},
				dateDefault: Number(new Date()),
				dateShow: false,
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad() {
			
		},
		methods: {
			onBirthday(eve) {
				this.formData.birthday = getNowTime(eve.value, 3)
				this.dateShow = false;
				console.log(getNowTime(eve.value, 3),"选择生日");
			},
			onSubmit() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			onLogout() {
				uni.reLaunch({
					url: "/pages/login/login"
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.radio-box {
		/deep/ .u-radio-group--row {
			justify-content: flex-end;
		}
	}
	.btn-box {
		.btn {
			width: 560rpx;
			height: 84rpx;
			background: #FF7171;
			border-radius: 12rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 32rpx;
			color: #FFFFFF;
		}
	}
	.cell-li {
		.li-label {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;
		}
		.place-txt {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #999999;
		}
	}
</style>
<style>
	page {
		background: #F5F5F5;
	}
</style>
